/*
 * fv3tk
 * Copyright (C) 2009  John Pritchard, jdp@syntelos.org
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package modelviewer;

import fv3ds.Model;
import fv3ds.Fv3Exception;

import fv3tk.Animator;
import fv3tk.Fv3Screen;

import javax.media.opengl.GL2;

import java.io.File;
import java.io.IOException;

/**
 * Model viewer based on multiple related programs including the work
 * of G.B. Rodgers.
 * @author jdp
 */
public class Main
    extends fv3tk.Fv3Canvas
{
    private final static float LightPosition[] = { 0, 50000000, 0, 1.0f };
    private final static float[] LightAmbient = {0.3f, 0.3f, 0.3f, 1.0f};
    private final static float[] LightDiffuse = {0.5f, 0.5f, 0.5f, 1.0f};
    private final static float[] LightSpecular = {0.5f, 0.5f, 0.5f, 1.0f};
    private final static float[] ModelAmbient = {0.5f, 0.5f, 0.5f, 1.0f};


    private final Model model;

    private float scale = 1.0f;
    private float rotX = 0.0f;
    private float rotY = 0.0f;


    public Main(File file)
        throws IOException, Fv3Exception
    {
        super();
        this.model = new Model(file);
    }

    public void init(GL2 gl){
        
        gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, ModelAmbient, 0);
        gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, LightPosition, 0);
        gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, LightDiffuse, 0);
        gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, LightAmbient, 0);
        gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, LightSpecular, 0);

        gl.glEnable(GL2.GL_LIGHT0);
        gl.glEnable(GL2.GL_LIGHTING);
        gl.glEnable(GL2.GL_NORMALIZE);

        gl.glEnable(GL2.GL_CULL_FACE);
        gl.glShadeModel(GL2.GL_SMOOTH);
        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        gl.glClearDepth(1.0f);
        gl.glEnable(GL2.GL_DEPTH_TEST);
        gl.glDepthFunc(GL2.GL_LEQUAL);
        gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);

        Fv3Screen fv3s = Fv3Screen.Current();
        float aspect = (float)(fv3s.width / fv3s.height);

        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(-1, 1, -1, 1, -50, 50);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }
    public void step(long time, long dt){


        if (Math.abs(this.rotX) >= 360.0f)
            this.rotX = 0.0f;

        if (Math.abs(this.rotY) >= 360.0f)
            this.rotY = 0.0f;
    }
    public void display(GL2 gl){
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        this.glu.gluLookAt(0,0,10, 0,0,0, 0,1,0);
        gl.glPushMatrix();
        gl.glScalef(this.scale,this.scale,this.scale);
        gl.glRotatef(this.rotY, 1.0f, 0.0f, 0.0f);
        gl.glRotatef(this.rotX, 0.0f, 1.0f, 0.0f);
        
        /*
         * Render model
         */

        gl.glPopMatrix();
            
        gl.glFlush();
    }

    public static void main(String[] argv){
        String filename = null;

        if (1 == argv.length)
            filename = argv[0];
        else 
            filename = System.getProperty("modelviewer.Main.Filename");//(TEMP)

        if (null != filename){
            File file = new File(filename);
            if (file.isFile()){
                try {
                    Main main = new Main(file);
                    Animator animator = new Animator(main);
                    animator.start();
                }
                catch (Exception exc){
                    exc.printStackTrace();
                    System.exit(1);
                }
            }
            else {
                System.err.println("Error: file not found '"+file.getPath()+"'.");
                System.exit(1);
            }
        }
        else {
            System.err.println("Usage:  modelviewer.Main  <model.file> ");
            System.exit(1);
        }
    }
}
